<?php

namespace app\admin\controller;

use app\admin\model\User;
use app\common\LogService;

class System extends Base
{
    public function main()
    {
        $data = config('sys_main');
        return view('main', ['data' => $data]);
    }
    public function link()
    {
        $data = config('sys_link');
        return view('link', ['data' => $data]);
    }
    public function upgrade()
    {
        $data = config('sys_upgrade');
        return view('upgrade', ['data' => $data]);
    }
    public function notice()
    {
        $data = config('sys_notice');
        return view('notice', ['data' => $data]);
    }
    public function password()
    {
        return view('password');
    }
    public function changePwd()
    {
        $user = User::where(['id' => $this->uid])->find();
        $param = input('post.');
        try {
            validate(['oldPassword|旧密码' => 'require', 'password|新密码' => 'require|min:5|max:11|alphaNum', 'repassword' => 'require'])->check($param);
            if (encrypt_pwd($param['oldPassword']) != $user['password'])  return error('旧密码错误');
            if ($param['password'] != $param['repassword']) return error('两次密码输入不一致');
            $res = User::where(['id' => $this->uid])->update(['password' => encrypt_pwd($param['password'])]);
            LogService::record(
                user_id: $this->uid,
                name: '修改密码',
                user_type: 2
            );
            if ($res) return success();
            return error();
        } catch (\think\exception\ValidateException $e) {
            return error($e->getMessage());
        }
    }
    // 邮箱服务
    public function mail()
    {
        $data = getConfig('admin','sys_mail');
        return view('mail' , ['data' => $data]);
    }
    // 支付配置
    public function pay()
    {
        // 1 易支付 2 面对面(支付宝)
        $confData = config('sys_pay');
        return view('pay', ['data' => $confData]);
    }
    public function update($conf)
    {
        $arr = ['sys_main', 'sys_notice', 'sys_upgrade', 'sys_pay','sys_mail','sys_link'];
        $recordLog = function ($status, $message = '') use ($conf) {
            LogService::record(
                name: '修改系统配置',
                target_id: $conf,
                user_id: $this->uid,
                status: $status,
                content: $message == '' ? [] : $message
            );
        };
        if (!in_array($conf, $arr)) {
            $recordLog(0, ['reason' => '请求异常']);
            return error('请求异常');
        }
        $original_data = config($conf);

        if (request()->isPost()) {
            $data = input();
            // 配置文件路径
            $configFile = getAppConfPath('admin') . $conf . '.php';
            // 生成新的配置文件内容
            $content = "<?php return " . var_export($data, true) . ";";

            // 写入配置文件
            if (file_put_contents($configFile, $content)) {
                $recordLog(1, ['original_data' => $original_data]);
                return success();
            } else {
                $recordLog(0);
                return error('配置更新失败');
            }
        }
    }
    // 上传图片
    public function uploadImg()
    {
        $file =  request()->file();
        if (!$file) {
            return error('请上传jpg,png,jpeg后缀图片');
        }
        try {
            validate(['images' => 'fileSize:5242880|fileExt:jpg,png,jpeg'])
                ->check($file);
            if (empty($file)) return error();
            $images = \think\facade\Filesystem::disk('images')->putFile('system', $file['images']);
            if (!$images) return error('图片上传失败');
            $images = '/static/images/' . str_replace('\\', '/', $images);
            return success($images);
        } catch (\think\exception\ValidateException $e) {
            return error($e->getMessage());
        }
    }
}
